From 4b555d6ea95dc4f962658eeb4e3b5d677b652434 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 29 Jul 2015 10:25:22 -0700 Subject: [PATCH] Use filetime for set_file_times Don't vendor the implementation locally. Closes #1859 --- Cargo.lock | 9 ++++-- tests/support/paths.rs | 64 ++++-------------------------------------- 2 files changed, 13 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aeb60472a..1f1da6d19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ dependencies = [ "curl 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "docopt 0.6.69 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "filetime 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "filetime 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "git2-curl 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -112,8 +112,13 @@ dependencies = [ [[package]] name = "filetime" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "flate2" diff --git a/tests/support/paths.rs b/tests/support/paths.rs index 4f585645e..57f59d712 100644 --- a/tests/support/paths.rs +++ b/tests/support/paths.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; use std::sync::{Once, ONCE_INIT}; use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; -use filetime::FileTime; +use filetime::{self, FileTime}; static CARGO_INTEGRATION_TEST_DIR : &'static str = "cit"; static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT; @@ -110,72 +110,20 @@ impl CargoPathExt for Path { let stat = try!(path.c_metadata()); let mtime = FileTime::from_last_modification_time(&stat); - let newtime = mtime.seconds() - 3600; + let newtime = mtime.seconds_relative_to_1970() - 3600; + let nanos = mtime.nanoseconds(); + let newtime = FileTime::from_seconds_since_1970(newtime, nanos); // Sadly change_file_times has a failure mode where a readonly file // cannot have its times changed on windows. - match set_file_times(path, newtime, newtime) { + match filetime::set_file_times(path, newtime, newtime) { Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => {} e => return e, } let mut perms = stat.permissions(); perms.set_readonly(false); try!(fs::set_permissions(path, perms)); - set_file_times(path, newtime, newtime) - } - - #[cfg(unix)] - fn set_file_times(p: &Path, atime: u64, mtime: u64) -> io::Result<()> { - use std::os::unix::prelude::*; - use std::ffi::CString; - use libc::{timeval, time_t, c_char, c_int}; - - let p = try!(CString::new(p.as_os_str().as_bytes())); - let atime = timeval { tv_sec: atime as time_t, tv_usec: 0, }; - let mtime = timeval { tv_sec: mtime as time_t, tv_usec: 0, }; - let times = [atime, mtime]; - extern { - fn utimes(name: *const c_char, times: *const timeval) -> c_int; - } - unsafe { - if utimes(p.as_ptr(), times.as_ptr()) == 0 { - Ok(()) - } else { - Err(io::Error::last_os_error()) - } - } - } - - #[cfg(windows)] - fn set_file_times(p: &Path, atime: u64, mtime: u64) -> io::Result<()> { - use std::fs::OpenOptions; - use std::os::windows::prelude::*; - use winapi::{FILETIME, DWORD}; - use kernel32; - - let f = try!(OpenOptions::new().write(true).open(p)); - let atime = to_filetime(atime); - let mtime = to_filetime(mtime); - return unsafe { - let ret = kernel32::SetFileTime(f.as_raw_handle() as *mut _, - 0 as *const _, - &atime, &mtime); - if ret != 0 { - Ok(()) - } else { - Err(io::Error::last_os_error()) - } - }; - - fn to_filetime(seconds: u64) -> FILETIME { - // FILETIME is a count of 100ns intervals, and there are 10^7 of - // these in a second - let seconds = seconds * 10_000_000; - FILETIME { - dwLowDateTime: seconds as DWORD, - dwHighDateTime: (seconds >> 32) as DWORD, - } - } + filetime::set_file_times(path, newtime, newtime) } } -- 2.30.2